var client = {};
var app = getApp();

// 获取房源订单信息接口
client.getConfirmRoomInfo = function (houseId, that) {
  my.request({
    url: app.globalData.url + "fc0856f0-a84b-44f6-ab4f-64711204bd50",
    method: "POST",
    data: {
      houseId: houseId
    },
    headers: {
      "Access-Token": app.globalData.token
    },
    // 期望返回的数据格式，默认json，支持json，text，base64
    dataType: "json",
    // 调用成功的回调函数
    success: function (res) {
      if (res.data.succeed == 0) {
        that.setData({
          buyerList: res.data.data.list,
          index: res.data.data.index,
          buildingName: res.data.data.buildingName,
          houseNo: res.data.data.houseNo,
          area: res.data.data.area,
          price: res.data.data.price
        });
      } else {
        app.errorHandling("获取" + my.getStorageSync({ key: 'taskType' }).data + "订单信息失败：", res);
      }
    },
    // 调用失败的回调函数
    fail: function (res) {
      my.showToast({
        content: "网络错误：" + res.data.errorMsg,
        duration: 2000
      });
    },
    complete: function () { }
  });
};

// 提交电子签名接口
client.submitSignature = function (houseId, that) {
  console.log("secondClick   ", that.data.secondClick);
  // 防止连续点击
  if (!that.data.secondClick) {
    return;
  }
  that.setData({
    secondClick: false
  });
  my.showLoading({
    content: '加载中...'
  });
  my.request({
    url: app.globalData.url + "22bff2e1-d15e-49af-ab21-82714f062552",
    method: "POST",
    data: {
      houseId: houseId
    },
    headers: {
      "Access-Token": app.globalData.token
    },
    // 期望返回的数据格式，默认json，支持json，text，base64
    dataType: "json",
    // 调用成功的回调函数
    success: function (res) {
      console.log(JSON.stringify(res));
      if (res.data.succeed == 0) {
        my.showToast({
          content: "恭喜您! 选" + my.getStorageSync({ key: 'taskType' }).data + "成功! ",
          duration: 2000
        });
        // 当前楼盘的当前用户选房成功，3 - 已选中"
        var taskId = my.getStorageSync({ key: 'taskId' }).data;
        my.setStorageSync({ key: 'buyerStatus' + taskId, data: 3 });
        my.setStorageSync({ key: 'mineHouseId' + taskId, data: houseId });
        getOrderDetailFromCreate(res.data.data, that);
      } else {
        app.errorHandling("选" + my.getStorageSync({ key: 'taskType' }).data + "失败：", res);
      }
    },
    // 调用失败的回调函数
    fail: function (res) {
      my.showToast({
        content: "网络错误：" + res.data.errorMsg,
        duration: 2000
      });
    },
    complete: function () {
      my.hideLoading();
      that.setData({
        secondClick: true
      });
    }
  });
};

// 创建订单后获取订单详情接口
function getOrderDetailFromCreate(orderNo, that) {
  my.showLoading({
    content: '加载中...'
  });
  my.request({
    url: app.globalData.url + "32614eb0-6c99-4b71-9fb8-d69aa7661cb3",
    method: "POST",
    data: {
      loginToken: app.globalData.token,
      orderNo: orderNo
    },
    headers: {
      "Access-Token": app.globalData.token
    },
    // 期望返回的数据格式，默认json，支持json，text，base64
    dataType: "json",
    // 调用成功的回调函数
    success: function (res) {
      console.log(JSON.stringify(res));
      if (res.data.succeed == 0) {
        // 如果支付金额为0，则直接显示0订单
        if (res.data.data.actuallyPayAmount == 0) {
          my.redirectTo({
            url: "../orderDetail/orderDetail?orderNo=" + orderNo
          });
        } else {
          // 如果支付金额不为0，则跳转到支付倒计时页面
          my.redirectTo({
            url:
              "../selectSuccess/selectSuccess?orderNo=" + res.data.data.orderNo
          });
        }
      } else {
        app.errorHandling("获取订单详情失败：", res);
      }
    },
    // 调用失败的回调函数
    fail: function (res) {
      my.showToast({
        content: "网络错误：" + res.data.errorMsg,
        duration: 2000
      });
    },
    complete: function () {
      my.hideLoading();
    }
  });
};

module.exports = {
  client: client
};